package com.ruoyi.plasma.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tongji.run.manager.ManagerFactory;
import org.apache.commons.lang3.ArrayUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plasma.domain.TBimsDonor;
import com.ruoyi.plasma.service.ITBimsDonorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 浆员管理Controller
 *
 * @author 孙哲
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/plasma/donor")
public class TBimsDonorController extends BaseController
{
    @Autowired
    private ITBimsDonorService tBimsDonorService;

    /**
     * 查询浆员管理列表
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:list')")
    @GetMapping("/list")
    public AjaxResult list(TBimsDonor tBimsDonor)
    {
        PageUtils.startPage(1,2000,true);
        List<TBimsDonor> list = tBimsDonorService.selectTBimsDonorList(tBimsDonor);

        SysUser user = SecurityUtils.getLoginUser().getUser();
        System.out.println(user.getUserName());
        SQLManager taibangSqlManager = ManagerFactory.taibangSqlManager();
        String linkmanName = user.getUserName();

        String xuanchuanSql = "select lr.linkmanName as chezhuname, d.*,flow.recordDate,flow.processNo,flow.plasmaCollection " +
                "from t_bims_donor_collect_flow flow " +
                "left join t_bims_donor d on flow.donorId = d.donorId " +
                "left join t_bims_linkman_donor ld on (d.donorId = ld.donorId and ld.flag = 'Y' and ld.relationType = 'conduct') " +
                "left join t_bims_linkman l on ld.linkmanId = l.linkmanId " +
                "left join t_bims_donor_register r on flow.donateNo = r.donateNo " +
                "left join t_bims_linkman lr on r.linkmanId = lr.linkmanId " +
                "where  l.linkmanName like ? ";
        String chezhuSql = "select xuanchuanyuan.linkmanName as xuanchuanyuanname, flow.recordDate as regDate,flow.processNo,flow.plasmaCollection,d.* " +
                "from t_bims_donor_collect_flow flow " +
                "left join t_bims_donor_register r on flow.donateNo = r.donateNo " +
                "left join t_bims_donor d on flow.donorId = d.donorId " +
                "left join t_bims_linkman chezhu on r.linkmanId = chezhu.linkmanId " +
                "left join t_bims_linkman_donor ld on (d.donorId = ld.donorId and ld.flag = 'Y' and ld.relationType = 'conduct') " +
                "left join t_bims_linkman as xuanchuanyuan on ld.linkmanId = xuanchuanyuan.linkmanId " +
                "where chezhu.linkmanName like ?";
        Object[] params = new Object[] {linkmanName};

        String beginPlasmaDate = (String)tBimsDonor.getParams().get("beginPlasmaDate");
        String endPlasmaDate = (String)tBimsDonor.getParams().get("endPlasmaDate");
        if (StringUtils.isNotBlank(beginPlasmaDate) && StringUtils.isNotBlank(endPlasmaDate)) {
            xuanchuanSql = xuanchuanSql + "and flow.recordDate between ? and ? ";
            chezhuSql = chezhuSql + "and flow.recordDate between ? and ? ";
            params = ArrayUtils.add(params, beginPlasmaDate);
            params = ArrayUtils.add(params, endPlasmaDate);
        }

        String plasmaDate = tBimsDonor.getPlasmaDate();
        if (StringUtils.isNotBlank(plasmaDate)) {
            xuanchuanSql = xuanchuanSql + "and flow.recordDate between ? and ? ";
            chezhuSql = chezhuSql + "and flow.recordDate between ? and ? ";
            params = ArrayUtils.add(params, plasmaDate);
            params = ArrayUtils.add(params, plasmaDate);
        }
        String donorName = tBimsDonor.getDonorName();
        if (StringUtils.isNotBlank(donorName)) {
            xuanchuanSql = xuanchuanSql + "and d.donorName like ? ";
            chezhuSql = chezhuSql + "and d.donorName like ? ";
            params = ArrayUtils.add(params, "%" + donorName + "%");
        }

        String donorNo = tBimsDonor.getDonorNo();
        if (StringUtils.isNotBlank(donorNo)) {
            xuanchuanSql = xuanchuanSql + "and d.donorNo like ? ";
            chezhuSql = chezhuSql + "and d.donorNo like ? ";
            params = ArrayUtils.add(params, donorNo);
        }

        String order = " order by d.plasmaDate asc";

        String pageNum = ServletUtils.getParameter(TableSupport.PAGE_NUM);
        String pageSize = ServletUtils.getParameter(TableSupport.PAGE_SIZE);

        xuanchuanSql = xuanchuanSql + " order by remark desc, d.plasmaDate asc";
        chezhuSql = chezhuSql + " order by xuanchuanyuanname desc, d.plasmaDate asc";

        List<HashMap> xuanchuantList = taibangSqlManager.execute(new SQLReady(xuanchuanSql, params), HashMap.class);

        List<HashMap> chezhuList = taibangSqlManager.execute(new SQLReady(chezhuSql, params), HashMap.class);

        HashMap data = new HashMap();
        data.put("xuanchuanList", xuanchuantList);
        data.put("chezhuList", chezhuList);

        return AjaxResult.success(data);
    }

    /**
     * 导出浆员管理列表
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:export')")
    @Log(title = "浆员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBimsDonor tBimsDonor)
    {
        List<TBimsDonor> list = tBimsDonorService.selectTBimsDonorList(tBimsDonor);
        ExcelUtil<TBimsDonor> util = new ExcelUtil<TBimsDonor>(TBimsDonor.class);
        util.exportExcel(response, list, "浆员管理数据");
    }

    /**
     * 获取浆员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:query')")
    @GetMapping(value = "/{donorId}")
    public AjaxResult getInfo(@PathVariable("donorId") String donorId)
    {
        return success(tBimsDonorService.selectTBimsDonorByDonorId(donorId));
    }

    /**
     * 新增浆员管理
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:add')")
    @Log(title = "浆员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBimsDonor tBimsDonor)
    {
        return toAjax(tBimsDonorService.insertTBimsDonor(tBimsDonor));
    }

    /**
     * 修改浆员管理
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:edit')")
    @Log(title = "浆员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBimsDonor tBimsDonor)
    {
        return toAjax(tBimsDonorService.updateTBimsDonor(tBimsDonor));
    }

    /**
     * 删除浆员管理
     */
    @PreAuthorize("@ss.hasPermi('plasma:donor:remove')")
    @Log(title = "浆员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{donorIds}")
    public AjaxResult remove(@PathVariable String[] donorIds)
    {
        return toAjax(tBimsDonorService.deleteTBimsDonorByDonorIds(donorIds));
    }
}
